package com.meichis.mcsappframework.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.meichis.mcsappframework.photoview.ZoomImageActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by pei on 2016/9/13.
 */
public class ZoomImageView extends ImageView implements View.OnClickListener {

    private String[] imagePaths;
    private int position;
    private int resId;

    public ZoomImageView(Context context) {
        super(context);
        setOnClickListener(this);
    }

    public ZoomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        resId = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","src",0);
        setOnClickListener(this);
    }

    public ZoomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        resId = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android","src",0);
        setOnClickListener(this);
    }

    public void displayImage(int resId) {
        imagePaths = null;
        this.resId = resId;
        this.setImageResource(resId);
    }

    public void displayImage(String imagePath, DisplayImageOptions options) {
        displayImage(new String[]{imagePath}, 0, options);
    }

    public void displayImage(String[] imagePaths, int position, DisplayImageOptions options) {
        resId = 0;
        this.imagePaths = imagePaths;
        this.position = position;
        if (imagePaths != null && imagePaths.length > 0 && imagePaths[position].length() > 8)
            ImageLoader.getInstance().displayImage(imagePaths[position], this, options);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getContext(), ZoomImageActivity.class);
        if (imagePaths != null && imagePaths.length > 0) {
            intent.putExtra("imagePaths", imagePaths);
            intent.putExtra("position", position);
        } else {
            intent.putExtra("resId", resId);
        }
        getContext().startActivity(intent);
    }
}
